//
//  HomeMainCell.m
//  B2C
//
//  Created by xiaowu on 2017/7/25.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "HomeMainCell.h"

@interface HomeMainCell ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UIImageView *headImageView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) UIView *footerView;
@end

@implementation HomeMainCell

- (void)awakeFromNib {
    [super awakeFromNib];

    [self configureUI];
}

#pragma mark - Method
- (void)configureUI {
    self.contentView.backgroundColor = [UIColor backgroundColor];
    self.tableView.backgroundColor = [UIColor backgroundColor];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.headView.size = CGSizeMake(ScreenWidth, 0.5 * ScreenWidth + 10);
    self.headImageView.frame = CGRectMake(0, 0, ScreenWidth, ScreenWidth * 0.5);
    [self.headView addSubview:self.headImageView];
    self.tableView.tableHeaderView = self.headView;
    [self.tableView registerClass:[CommodityCell class] forCellReuseIdentifier:@"cell"];
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (weakSelf.headerRefresh) {
            weakSelf.headerRefresh();
        }
    }];
    
    self.tableView.tableFooterView = self.footerView;

}

- (void)setShowArr:(NSArray *)arr headImage:(NSString *)imageUrl {

//    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    if (self.tableView.mj_header.isRefreshing) {
        [self.tableView.mj_header endRefreshing];
    } else {
        [self.tableView scrollRectToVisible:CGRectMake(0, self.tableView.origin.y, self.tableView.width, self.tableView.height) animated:NO];
    }

    
    self.dataArr = [NSMutableArray arrayWithArray:arr];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:PlaceholderImageBanner];
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    HomeOthersModel *model = [self.dataArr safeObjectAtIndex:section];
    return [CommodityCell getRowNumberWithItemArr:model.goods];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommodityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor backgroundColor];
    HomeOthersModel *model = [self.dataArr safeObjectAtIndex:indexPath.section];
    [cell setValueForCell:[CommodityCell getCellItemArr:model.goods withRow:indexPath.row]];
    __weak typeof(self) weakSelf = self;
    cell.block = ^(NSInteger index) {
        CommodityModel *commodity = [[CommodityCell getCellItemArr:model.goods withRow:indexPath.row] safeObjectAtIndex:index];
        if (weakSelf.block) {
            weakSelf.block(commodity.gid);
        }
    };
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [CommodityCell getHeight];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HomeOthersModel *model = [self.dataArr safeObjectAtIndex:section];

    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = model.gtcname;
    
    return label;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

#pragma mark - Getters
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (UIView *)headView {
    if (!_headView) {
        _headView = [UIView new];
    }
    return _headView;
}

- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
    }
    return _headImageView;
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 60, 0.5)];
        
        lineView.backgroundColor = [UIColor grayTextColor];
        lineView.center = _footerView.center;
        
        NSString *footStr = @"再怎么翻也没有了";
        NSDictionary *attributes = @{NSFontAttributeName:TextNormalFont12};
        CGFloat length = [footStr boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        length = ceilf(length);
        
        // 文字
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, length + 30, 40)];
        titleLabel.text = footStr;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = TextNormalFont12;
        titleLabel.textColor = [UIColor grayTextColor];
        titleLabel.backgroundColor = [UIColor backgroundColor];
        
        titleLabel.center = lineView.center;
        
        
        [_footerView addSubview:lineView];
        [_footerView addSubview:titleLabel];
    }
    return _footerView;
}

@end
